package com.yf.stat.modules.repo.controller;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.yf.ability.excel.ExportExcel;
import com.yf.base.api.api.ApiRest;
import com.yf.base.api.api.controller.BaseController;
import com.yf.base.api.api.dto.PagingReqDTO;
import com.yf.stat.modules.repo.dto.request.StatRepoTotalReqDTO;
import com.yf.stat.modules.repo.dto.response.StatRepoTotalDTO;
import com.yf.stat.modules.repo.service.StatRepoService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.log4j.Log4j2;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletResponse;
import java.util.List;

/**
* <p>
* 题库统计总览
* </p>
*
* @author 聪明笨狗
* @since 2020-09-11 11:10
*/
@Log4j2
@Api(tags={"题库统计总览"})
@RestController
@RequestMapping("/api/stat/repo")
public class StatRepoController extends BaseController {

    @Autowired
    private StatRepoService baseService;

    @Autowired
    private ExportExcel exportExcel;


    /**
     * 题库统计总览
     * @param reqDTO
     * @return
     */
    @ApiOperation(value = "题库统计总览")
    @PostMapping("/repo-overview-paging")
    public ApiRest<IPage<StatRepoTotalDTO>> repoPreviewPaging(@RequestBody PagingReqDTO<StatRepoTotalReqDTO> reqDTO) {
        IPage<StatRepoTotalDTO> dto = baseService.repoPreviewPaging(reqDTO);
        return super.success(dto);
    }


    /**
     * 题库统计总览导出
     * @param reqDTO
     * @return
     */
    @ApiOperation(value = "题库统计总览导出")
    @PostMapping("/repo-overview-export")
    public ApiRest<IPage<StatRepoTotalDTO>> repoPreviewExport(HttpServletResponse response, @RequestBody StatRepoTotalReqDTO reqDTO) {

        PagingReqDTO<StatRepoTotalReqDTO> query = new PagingReqDTO<>();
        query.setCurrent(1);
        query.setSize(Integer.MAX_VALUE);
        query.setParams(reqDTO);

        IPage<StatRepoTotalDTO> dto = baseService.repoPreviewPaging(query);

        List<StatRepoTotalDTO> list = dto.getRecords();

        // 执行导出
        exportExcel.export(response, StatRepoTotalDTO.class, "题库总览统计", list);


        return super.success(dto);
    }

}
